<?php
Yii::import('ext.helpers.EDownloadHelper');
class CatalogsController extends Controller
{

	public $layout='//layouts/main';

	public function actionIndex()
	{
		$criteria = new CDbCriteria;
    $criteria -> order = 'created_at DESC';

    //Pagination
    $catalogs = Catalogs::model()->findAll('catalog_category = :catalog_category', array(':catalog_category' => 1));
    $count = count($catalogs);

    $pages = new CPagination($count);
    $pages -> pageSize = 10;
    $pages -> applyLimit($criteria);

		$this->render('index', array(
      'catalogs' => $catalogs,
      'pages' => $pages,
    ));
	}

  public function actionInternational()
	{
		$criteria = new CDbCriteria;
    $criteria -> order = 'created_at DESC';

    //Pagination
    $catalogs = Catalogs::model()->findAll('catalog_category != :catalog_category', array(':catalog_category' => 1));
    $count = count($catalogs);

    $pages = new CPagination($count);
    $pages -> pageSize = 10;
    $pages -> applyLimit($criteria);

		$this->render('index', array(
      'catalogs' => $catalogs,
      'pages' => $pages,
    ));
	}

  public function actionDownload()
	{
		$catalog = Catalogs::model()->find('id = :id', array(':id'=>$_GET['id']));
		$path = bu('assets/catalogs/').$catalog->content;

		EDownloadHelper::download(Yii::getPathOfAlias('webroot.assets.catalogs').DIRECTORY_SEPARATOR.$catalog->content);
	}

	public function loadModel($id)
	{
		$model=Catalogs::model()->findByPk((int)$id);
		if($model===null)
			throw new CHttpException(404,'The requested page does not exist.');
		return $model;
	}

	/**
	 * Performs the AJAX validation.
	 * @param CModel the model to be validated
	 */
	protected function performAjaxValidation($model)
	{
		if(isset($_POST['ajax']) && $_POST['ajax']==='catalogs-form')
		{
			echo CActiveForm::validate($model);
			Yii::app()->end();
		}
	}
}
